package org.example.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.order.entity.Order;
import org.example.order.mapper.OrderMapper;
import org.example.order.service.OrderService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Override
    public void add(Order order) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        order.setOrderTime(LocalDateTime.now().format(formatter));
        baseMapper.insert(order);
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            baseMapper.deleteById(id);
        }
    }

    @Override
    public void update(Order order) {
        baseMapper.updateById(order);
    }

    @Override
    public Order selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Order> selectAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public PageInfo<Order> selectPage(Order order, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (order != null && order.getOrderNumber() != null) {
            wrapper.like("order_number", order.getOrderNumber());
        }
        List<Order> list = baseMapper.selectList(wrapper);
        return new PageInfo<>(list);
    }
}
